package logic

import (
	"context"
	"fmt"

	"server_1.0/service/major/internal/svc"
	"server_1.0/service/major/major"
	"server_1.0/service/major/model"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInsertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertLogic {
	return &InsertLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *InsertLogic) Insert(in *major.InsertReq) (*major.QReps, error) {
	ID := fmt.Sprintf("%s%s", in.ID, in.No)
	code := util.Success
	usersHandle := make([]*major.Reps, 0)
	count := 0
	newMaj := model.Major{
		Mno:   ID,
		Mname: in.Name,
		Mcno:  in.ID,
	}
	_, err := l.svcCtx.MajorModel.Insert(l.ctx, &newMaj)
	if err != nil {
		code = util.AddError
	}
	usersHandle = append(
		usersHandle,
		&major.Reps{
			Code: int64(code),
			No:   in.No,
			Name: in.Name,
		})
	count += 1
	return &major.QReps{
		Count: int64(count),
		QRs:   usersHandle,
	}, nil
}
